package com.athena.utils 
{
	import com.athena.view.AlertPopup;
	import com.athena.view.ConfirmPopup;
	import com.athena.view.Loadding;
	import com.junkbyte.console.Cc;
	import fl.motion.AdjustColor;
	import flash.filters.ColorMatrixFilter;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author chau
	 */
	public class Util 
	{
		
		public function Util() 
		{
			
		}
		public static function simpleAlert(str : String) : void {
			var pop : AlertPopup = AlertPopup(Main.getInstance().getChildByName(AlertPopup.POPUPNAME));
			if (pop) Main.getInstance().removeChild(pop);
			pop = new AlertPopup(str);
			Main.getInstance().addChild(pop);
		}
		
		public static function simpleConfirm(str : String, _callback : Object = null) : void {
			var pop : ConfirmPopup = ConfirmPopup(Main.getInstance().getChildByName(ConfirmPopup.POPUPNAME));
			if (pop) Main.getInstance().removeChild(pop);
			pop = new ConfirmPopup(str);
			pop.addEventListener(ConfirmPopup.ACCEPT, _call);
			Main.getInstance().addChild(pop);
			function _call() : void {
				if (_callback != null )
					_callback.call();
			}
		}
		
		public static function navigateUrl(url : String, type : String  = '_blank') : void {
			var request:URLRequest = new URLRequest(url);
			try {
				navigateToURL(request, type);
			} catch (e:Error) {
				trace("Error occurred!");
			}
		}
		public static function showLoading() : void {
			var load_mc : Loadding = Loadding(Main.getInstance().getChildByName(Loadding.LoaddingName));
			if (load_mc) Main.getInstance().removeChild(load_mc);
			load_mc = new Loadding();
			Main.getInstance().addChild(load_mc);
		}
		
		public static function removeLoad() : void {
			var load_mc : Loadding = Loadding(Main.getInstance().getChildByName(Loadding.LoaddingName));
			if (load_mc) {
				Main.getInstance().removeChild(load_mc);
				load_mc = null;
			}
		}
		public static function numberFormat(number:Number, maxDecimals:int = 2, forceDecimals:Boolean = false, siStyle:Boolean = true):String {
			var i:int = 0, inc:Number = Math.pow(10, maxDecimals), str:String = String(Math.round(inc * Number(number))/inc);
			var hasSep:Boolean = str.indexOf(",") == -1, sep:int = hasSep ? str.length : str.indexOf(",");
			var ret:String = (hasSep && !forceDecimals ? "" : (siStyle ? "." : ",")) + str.substr(sep+1);
			if (forceDecimals) for (var j:int = 0; j <= maxDecimals - (str.length - (hasSep ? sep-1 : sep)); j++) ret += "0";
			while (i + 3 < (str.substr(0, 1) == "-" ? sep-1 : sep)) ret = (siStyle ? "," : ".") + str.substr(sep - (i += 3), 3) + ret;
			return str.substr(0, sep - i) + ret;
		}
		public static function trim( s:String ):String	{
			return s.replace(/^\s+|\s+$/g, "");
		}
		public static function adjustButton(mc : *, bool : Boolean = true) : void {
			var colourFilter:AdjustColor = new AdjustColor();
			var mColourMatrix:ColorMatrixFilter;
			var mMatrix:Array;
			if (bool) {
				colourFilter.brightness = 85;
				colourFilter.contrast = -35;
				colourFilter.saturation = -80;
				colourFilter.hue = 0;
			}else {
				colourFilter.brightness = 0;
				colourFilter.contrast = 0;
				colourFilter.saturation = 0;
				colourFilter.hue = 0;
			}
			mMatrix = colourFilter.CalculateFinalFlatArray();
			mColourMatrix= new ColorMatrixFilter(mMatrix);

			mc.filters = [mColourMatrix];
		}
	}

}